home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / lang / string~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  25.4 KB  |  874 lines

  1. /*
  2.  * @(#)String.java    1.54 95/12/07  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. import java.util.Hashtable;
  23.  
  24. /**
  25.  * A general class of objects to represent character Strings.
  26.  * Strings are constant, their values cannot be changed after creation.
  27.  * The compiler makes sure that each String constant actually results
  28.  * in a String object. Because String objects are immutable they can
  29.  * be shared. For example:
  30.  * <pre>
  31.  *    String str = "abc";
  32.  * </pre>
  33.  * is equivalent to:
  34.  * <pre>
  35.  *    char data[] = {'a', 'b', 'c'};
  36.  *    String str = new String(data);
  37.  * </pre>
  38.  * Here are some more examples of how strings can be used:
  39.  * <pre>
  40.  *     System.out.println("abc");
  41.  *     String cde = "cde";
  42.  *     System.out.println("abc" + cde);
  43.  *    String c = "abc".substring(2,3);
  44.  *    String d = cde.substring(1, 2);
  45.  * </pre>
  46.  * @see        StringBuffer
  47.  * @version     1.54, 12/07/95
  48.  * @author     Lee Boynton
  49.  * @author    Arthur van Hoff
  50.  */
  51. public final
  52. class String {
  53.     /** The value is used for character storage. */
  54.     private char value[];
  55.  
  56.     /** The offset is the first index of the storage that is used. */
  57.     private int offset;
  58.  
  59.     /** The count is the number of characters in the String. */
  60.     private int count;
  61.  
  62.     /**
  63.      * Constructs a new empty String.
  64.      */
  65.     public String() {
  66.     value = new char[0];
  67.     }
  68.  
  69.     /**
  70.      * Constructs a new String that is a copy of the specified String.
  71.      * @param value the initial value of the String
  72.      */
  73.     public String(String value) {
  74.     count = value.length();
  75.     this.value = new char[count];
  76.     value.getChars(0, count, this.value, 0);
  77.     }
  78.  
  79.     /**
  80.      * Constructs a new String whose initial value is the specified array 
  81.      * of characters.
  82.      * @param value the initial value of the String
  83.      */
  84.     public String(char value[]) {
  85.     this.count = value.length;
  86.     this.value = new char[count];
  87.     System.arraycopy(value, 0, this.value, 0, count);
  88.     }
  89.  
  90.     /**
  91.      * Constructs a new String whose initial value is the specified sub array of characters.
  92.      * The length of the new string will be count characters
  93.      * starting at offset within the specified character array.
  94.      * @param value    the initial value of the String, an array of characters
  95.      * @param offset    the offset into the value of the String
  96.      * @param count     the length of the value of the String
  97.      * @exception StringIndexOutOfBoundsException If the offset and count arguments are invalid.
  98.      */
  99.     public String(char value[], int offset, int count) {
  100.     if (offset < 0) {
  101.         throw new StringIndexOutOfBoundsException(offset);
  102.     }
  103.     if (count < 0) {
  104.         throw new StringIndexOutOfBoundsException(count);
  105.     }
  106.     if (offset + count > value.length) {
  107.         throw new StringIndexOutOfBoundsException(offset + count);
  108.     }
  109.  
  110.     this.value = new char[count];
  111.     this.count = count;
  112.     System.arraycopy(value, offset, this.value, 0, count);
  113.     }
  114.  
  115.     /**
  116.      * Constructs a new String whose initial value is the specified sub array of bytes.
  117.      * The high-byte of each character can be specified, it should usually be 0.
  118.      * The length of the new String will be count characters
  119.      * starting at offset within the specified character array.  
  120.      * @param ascii    the bytes that will be converted to characters
  121.      * @param hibyte    the high byte of each Unicode character
  122.      * @param offset    the offset into the ascii array
  123.      * @param count     the length of the String
  124.      * @exception StringIndexOutOfBoundsException If the offset and count arguments are invalid.
  125.      */
  126.     public String(byte ascii[], int hibyte, int offset, int count) {
  127.     if (offset < 0) {
  128.         throw new StringIndexOutOfBoundsException(offset);
  129.     }
  130.     if (count < 0) {
  131.         throw new StringIndexOutOfBoundsException(count);
  132.     }
  133.     if (offset + count > ascii.length) {
  134.         throw new StringIndexOutOfBoundsException(offset + count);
  135.     }
  136.  
  137.     char value[] = new char[count];
  138.     this.count = count;
  139.     this.value = value;
  140.  
  141.     if (hibyte == 0) {
  142.         for (int i = count ; i-- > 0 ;) {
  143.         value[i] = (char) (ascii[i + offset] & 0xff);
  144.         }
  145.     } else {
  146.         hibyte <<= 8;
  147.         for (int i = count ; i-- > 0 ;) {
  148.         value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
  149.         }
  150.     }
  151.     }
  152.  
  153.     /**
  154.      * Constructs a new String whose value is the specified array of bytes.
  155.      * The byte array transformed into Unicode chars using hibyte
  156.      * as the upper byte of each character.
  157.      * @param ascii    the byte that will be converted to characters
  158.      * @param hibyte    the top 8 bits of each 16 bit Unicode character
  159.      */
  160.     public String(byte ascii[], int hibyte) {
  161.     this(ascii, hibyte, 0, ascii.length);
  162.     }
  163.  
  164.      
  165.     /**
  166.      * Construct a new string whose value is the current contents of the
  167.      * given string buffer
  168.      * @param buffer     the stringbuffer to be converted
  169.      */
  170.     public String (StringBuffer buffer) { 
  171.     synchronized(buffer) { 
  172.         buffer.setShared();
  173.         this.value = buffer.getValue();
  174.         this.offset = 0;
  175.         this.count = buffer.length();
  176.     }
  177.     }
  178.     
  179.  
  180.     /**
  181.      * Returns the length of the String.
  182.      * The length of the String is equal to the number of 16 bit
  183.      * Unicode characters in the String.
  184.      */
  185.     public int length() {
  186.     return count;
  187.     }
  188.  
  189.     /**
  190.      * Returns the character at the specified index. An index ranges
  191.      * from <tt>0</tt> to <tt>length() - 1</tt>.
  192.      * @param index    the index of the desired character
  193.      * @exception    StringIndexOutOfBoundsException If the index is not
  194.      *            in the range <tt>0</tt> to <tt>length()-1</tt>.
  195.      */
  196.     public char charAt(int index) {
  197.     if ((index < 0) || (index >= count)) {
  198.         throw new StringIndexOutOfBoundsException(index);
  199.     }
  200.     return value[index + offset];
  201.     }
  202.  
  203.     /**
  204.      * Copies characters from this String into the specified character array.
  205.      * The characters of the specified substring (determined by
  206.      * srcBegin and srcEnd) are copied into the character array,
  207.      * starting at the array's dstBegin location.
  208.      * @param srcBegin    index of the first character in the string
  209.      * @param srcEnd    end of the characters that are copied
  210.      * @param dst        the destination array
  211.      * @param dstBegin    the start offset in the destination array
  212.      */
  213.     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  214.     System.arraycopy(value, offset + srcBegin, dst, dstBegin, srcEnd - srcBegin);
  215.     }
  216.  
  217.     /**
  218.      * Copies characters from this String into the specified byte array.
  219.      * Copies the characters of the specified substring (determined by
  220.      * srcBegin and srcEnd) into the byte array, starting at the
  221.      * array's dstBegin location.
  222.      * @param srcBegin    index of the first character in the String
  223.      * @param srcEnd    end of the characters that are copied
  224.      * @param dst        the destination array
  225.      * @param dstBegin    the start offset in the destination array
  226.      */
  227.     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
  228.     int j = dstBegin;
  229.     int n = offset + srcEnd;
  230.     int i = offset + srcBegin;
  231.     while (i < n) {
  232.         dst[j++] = (byte)value[i++];
  233.     }
  234.     }
  235.  
  236.     /**
  237.      * Compares this String to the specified object.
  238.      * Returns true if the object is equal to this String; that is,
  239.      * has the same length and the same characters in the same sequence.
  240.      * @param anObject    the object to compare this String against
  241.      * @return     true if the Strings are equal; false otherwise.
  242.      */
  243.     public boolean equals(Object anObject) {
  244.     if ((anObject != null) && (anObject instanceof String)) {
  245.         String anotherString = (String)anObject;
  246.         int n = count;
  247.         if (n == anotherString.count) {
  248.         char v1[] = value;
  249.         char v2[] = anotherString.value;;
  250.         int i = offset;
  251.         int j = anotherString.offset;
  252.         while (n-- != 0) {
  253.             if (v1[i++] != v2[j++]) {
  254.             return false;
  255.             }
  256.         }
  257.         return true;
  258.         }
  259.     }
  260.     return false;
  261.     }
  262.  
  263.     /**
  264.      * Compares this String to another object.
  265.      * Returns true if the object is equal to this String; that is,
  266.      * has the same length and the same characters in the same sequence.
  267.      * Upper case characters are folded to lower case before
  268.      * they are compared.
  269.      * @param anotherString    the String to compare this String against
  270.      * @return     true if the Strings are equal, ignoring case; false otherwise.
  271.      */
  272.     public boolean equalsIgnoreCase(String anotherString) {
  273.     return (anotherString != null) && (anotherString.count == count) &&
  274.         regionMatches(true, 0, anotherString, 0, count);
  275.     }
  276.  
  277.     /**
  278.      * Compares this String to another specified String.
  279.      * Returns an integer that is less than, equal to, or greater than zero.
  280.      * The integer's value depends on whether this String is less than, equal to, or greater
  281.      * than anotherString.
  282.      * @param anotherString the String to be compared
  283.      */
  284.     public int compareTo(String anotherString) {
  285.     int len1 = count;
  286.     int len2 = anotherString.count;
  287.     int n = Math.min(len1, len2);
  288.     char v1[] = value;
  289.     char v2[] = anotherString.value;
  290.     int i = offset;
  291.     int j = anotherString.offset;
  292.  
  293.     while (n-- != 0) {
  294.         char c1 = v1[i++];
  295.         char c2 = v2[j++];
  296.         if (c1 != c2) {
  297.         return c1 - c2;
  298.         }
  299.     }
  300.     return len1 - len2;
  301.     }
  302.  
  303.     /**
  304.      * Determines whether a region of this String matches the specified region
  305.      * of the specified String.
  306.      * @param toffset    where to start looking in this String
  307.      * @param other     the other String
  308.      * @param ooffset    where to start looking in the other String
  309.      * @param len       the number of characters to compare
  310.      * @return          true if the region matches with the other; false otherwise.
  311.      */
  312.     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
  313.     char ta[] = value;
  314.     int to = offset + toffset;
  315.     int tlim = offset + count;
  316.     char pa[] = other.value;
  317.     int po = other.offset + ooffset;
  318.     int plim = po + other.count;
  319.     if ((ooffset < 0) || (toffset < 0) || (to + len > tlim) || (po + len > plim)) {
  320.         return false;
  321.     }
  322.     while (--len >= 0) {
  323.         if (ta[to++] != pa[po++]) {
  324.             return false;
  325.         }
  326.     }
  327.     return true;
  328.     }
  329.  
  330.     /**
  331.      * Determines whether a region of this String matches the specified region
  332.      * of the specified String.  If the boolean ignoreCase is true, upper case characters are 
  333.      * considered equivalent to lower case letters.
  334.      * @param ignoreCase if true, case is ignored
  335.      * @param toffset    where to start looking in this String
  336.      * @param other     the other String
  337.      * @param ooffset    where to start looking in the other String
  338.      * @param len       the number of characters to compare
  339.      * @return          true if the region matches with the other; false otherwise.
  340.      */
  341.     public boolean regionMatches(boolean ignoreCase,
  342.                          int toffset,
  343.                            String other, int ooffset, int len) {
  344.     char ta[] = value;
  345.     int to = offset + toffset;
  346.     int tlim = offset + count;
  347.     char pa[] = other.value;
  348.     char trt[] = Character.upCase;
  349.     int po = other.offset + ooffset;
  350.     int plim = po + other.count;
  351.     if ((ooffset < 0) || (toffset < 0) || (to + len > tlim) || (po + len > plim)) {
  352.         return false;
  353.     }
  354.     while (--len >= 0) {
  355.         int c1 = ta[to++];
  356.         int c2 = pa[po++];
  357.         if ((c1 != c2)
  358.             && (!ignoreCase ||
  359.             (c1 > 256) || (c2 > 256) ||
  360.             (trt[c1] != trt[c2]))) {
  361.         return false;
  362.         }
  363.     }
  364.     return true;
  365.     }
  366.  
  367.     /**
  368.      * Determines whether this String starts with some prefix.
  369.      * @param prefix    the prefix
  370.      * @param toffset    where to begin looking in the the String
  371.      * @return         true if the String starts with the specified prefix; false otherwise.
  372.      */
  373.     public boolean startsWith(String prefix, int toffset) {
  374.     char ta[] = value;
  375.     int to = offset + toffset;
  376.     int tlim = offset + count;
  377.     char pa[] = prefix.value;
  378.     int po = prefix.offset;
  379.     int pc = prefix.count;
  380.     int plim = po + pc;
  381.     if ((toffset < 0) || (to + pc > tlim)) {
  382.         return false;
  383.     }
  384.     while (--pc >= 0) {
  385.         if (ta[to++] != pa[po++]) {
  386.             return false;
  387.         }
  388.     }
  389.     return true;
  390.     }
  391.  
  392.     /**
  393.      * Determines whether this String starts with some prefix.
  394.      * @param prefix    the prefix
  395.      * @return         true if the String starts with the specified prefix; false otherwise. 
  396.      */
  397.     public boolean startsWith(String prefix) {
  398.     return startsWith(prefix, 0);
  399.     }
  400.  
  401.     /**
  402.      * Determines whether the String ends with some suffix.
  403.      * @param suffix    the suffix
  404.      * @return         true if the String ends with the specified suffix; false otherwise.
  405.      */
  406.     public boolean endsWith(String suffix) {
  407.     return startsWith(suffix, count - suffix.count);
  408.     }
  409.  
  410.     /**
  411.      * Returns a hashcode for this String. This is a large
  412.      * number composed of the character values in the String.
  413.      */
  414.     public int hashCode() {
  415.     int h = 0;
  416.     int off = offset;
  417.     char val[] = value;
  418.     int len = count;
  419.  
  420.     if (len < 16) {
  421.         for (int i = len ; i > 0; i--) {
  422.         h = (h * 37) + val[off++];
  423.         }
  424.     } else {
  425.         // only sample some characters
  426.         int skip = len / 8;
  427.         for (int i = len ; i > 0; i -= skip, off += skip) {
  428.         h = (h * 39) + val[off];
  429.         }
  430.     }
  431.     return h;
  432.     }
  433.  
  434.     /**
  435.      * Returns the index within this String of the first occurrence of the specified 
  436.      * character.  This method returns -1 if the index is not found.
  437.      * @param ch    the character to search for
  438.      */
  439.     public int indexOf(int ch) {
  440.     return indexOf(ch, 0);
  441.     }
  442.  
  443.     /**
  444.      * Returns the index within this String of the first occurrence of the specified 
  445.      * character, starting the search at fromIndex.  This method 
  446.      * returns -1 if the index is not found.
  447.      * @param ch    the character to search for
  448.      * @param fromIndex    the index to start the search from
  449.      */
  450.     public int indexOf(int ch, int fromIndex) {
  451.     int max = offset + count;
  452.     char v[] = value;
  453.  
  454.     for (int i = offset + fromIndex ; i < max ; i++) {
  455.         if (v[i] == ch) {
  456.         return i - offset;
  457.         }
  458.     }
  459.     return -1;
  460.     }
  461.  
  462.     /**
  463.      * Returns the index within this String of the last occurrence of the specified character.
  464.      * The String is searched backwards starting at the last character.
  465.      * This method returns -1 if the index is not found.
  466.      * @param ch    the character to search for
  467.      */
  468.     public int lastIndexOf(int ch) {
  469.     return lastIndexOf(ch, count - 1);
  470.     }
  471.  
  472.     /**
  473.      * Returns the index within this String of the last occurrence of the specified character.
  474.      * The String is searched backwards starting at fromIndex.
  475.      * This method returns -1 if the index is not found.
  476.      * @param ch    the character to search for
  477.      * @param fromIndex    the index to start the search from
  478.      */
  479.     public int lastIndexOf(int ch, int fromIndex) {
  480.     int min = offset;
  481.     char v[] = value;
  482.     
  483.     for (int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex) ; i >= min ; i--) {
  484.         if (v[i] == ch) {
  485.         return i - offset;
  486.         }
  487.     }
  488.     return -1;
  489.     }
  490.  
  491.     /**
  492.      * Returns the index within this String of the first occurrence of the specified substring.
  493.      * This method returns -1 if the index is not found.
  494.      * @param str     the substring to search for
  495.      */
  496.     public int indexOf(String str) {
  497.     return indexOf(str, 0);
  498.     }
  499.  
  500.     /**
  501.      * Returns the index within this String of the first occurrence of the specified substring.
  502.      * The search is started at fromIndex.
  503.      * This method returns -1 if the index is not found.
  504.      * @param str     the substring to search for
  505.      * @param fromIndex    the index to start the search from
  506.      */
  507.     public int indexOf(String str, int fromIndex) {
  508.     char v1[] = value;
  509.     char v2[] = str.value;
  510.     int max = offset + (count - str.count);
  511.       test:
  512.     for (int i = offset + ((fromIndex < 0) ? 0 : fromIndex); i <= max ; i++) {
  513.         int n = str.count;
  514.         int j = i;
  515.         int k = str.offset;
  516.         while (n-- != 0) {
  517.         if (v1[j++] != v2[k++]) {
  518.             continue test;
  519.         }
  520.         }
  521.         return i - offset;
  522.     }
  523.     return -1;
  524.     }
  525.  
  526.     /**
  527.      * Returns the index within this String of the last occurrence of the specified substring.
  528.      * The String is searched backwards.
  529.      * This method returns -1 if the index is not found.
  530.      * @param str     the substring to search for
  531.      */
  532.     public int lastIndexOf(String str) {
  533.     return lastIndexOf(str, count - 1);
  534.     }
  535.  
  536.     /**
  537.      * Returns the index within this String of the last occurrence of the specified substring.
  538.      * The String is searched backwards starting at fromIndex.
  539.      * This method returns -1 if the index is not found.
  540.      * @param str     the substring to search for
  541.      * @param fromIndex    the index to start the search from
  542.      */
  543.     public int lastIndexOf(String str, int fromIndex) {
  544.     char v1[] = value;
  545.     char v2[] = str.value;
  546.     int min = offset;
  547.       test:
  548.     for (int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex); i >= min ; i--) {
  549.         int n = str.count;
  550.         int j = i;
  551.         int k = str.offset;
  552.         while (n-- != 0) {
  553.         if (v1[j++] != v2[k++]) {
  554.             continue test;
  555.         }
  556.         }
  557.         return i - offset;
  558.     }
  559.     return -1;
  560.     }
  561.  
  562.     /**
  563.      * Returns the substring of this String. The substring is specified
  564.      * by a beginIndex (inclusive) and the end of the string.
  565.      * @param beginIndex the beginning index, inclusive
  566.      */
  567.     public String substring(int beginIndex) {
  568.     return substring(beginIndex, length());
  569.     }
  570.  
  571.     /**
  572.      * Returns the substring of a String. The substring is specified
  573.      * by a beginIndex (inclusive) and an endIndex (exclusive).
  574.      * @param beginIndex the beginning index, inclusive
  575.      * @param endIndex the ending index, exclusive
  576.      * @exception StringIndexOutOfBoundsException If the beginIndex or the endIndex is out 
  577.      * of range.
  578.      */
  579.     public String substring(int beginIndex, int endIndex) {
  580.     if (beginIndex > endIndex) {
  581.         int tmp = beginIndex;
  582.         beginIndex = endIndex;
  583.         endIndex = tmp;
  584.     }
  585.     if (beginIndex < 0) {
  586.         throw new StringIndexOutOfBoundsException(beginIndex);
  587.     } 
  588.     if (endIndex > count) {
  589.         throw new StringIndexOutOfBoundsException(endIndex);
  590.     }
  591.     return ((beginIndex == 0) && (endIndex == count)) ? this :
  592.            new String(value, offset + beginIndex, endIndex - beginIndex);
  593.     }
  594.  
  595.     /**
  596.      * Concatenates the specified string to the end of this String.
  597.      * @param str    the String which is concatenated to the end of this String
  598.      */
  599.     public String concat(String str) {
  600.     int otherLen = str.length();
  601.     if (otherLen == 0) {
  602.         return this;
  603.     }
  604.     char buf[] = new char[count + otherLen];
  605.     getChars(0, count, buf, 0);
  606.     str.getChars(0, otherLen, buf, count);
  607.     return new String(buf);
  608.     }
  609.  
  610.     /**
  611.      * Converts this String by replacing all occurences of oldChar with newChar.
  612.      * @param oldChar    the old character
  613.      * @param newChar    the new character
  614.      */
  615.     public String replace(char oldChar, char newChar) {
  616.     if (oldChar != newChar) {
  617.         int len = count;
  618.         int i = -1;
  619.         while (++i < len) {
  620.         if (value[offset + i] == oldChar) {
  621.             break;
  622.         }
  623.         }
  624.         if (i < len) {
  625.         char buf[] = new char[len];
  626.         for (int j = 0 ; j < i ; j++) {
  627.             buf[j] = value[offset+j];
  628.         }
  629.         while (i < len) {
  630.             char c = value[offset + i];
  631.             buf[i] = (c == oldChar) ? newChar : c;
  632.             i++;
  633.         }
  634.         return new String(buf);
  635.         }
  636.     }
  637.     return this;
  638.     }
  639.  
  640.     /**
  641.      * Converts all of the characters in this String to lower case.
  642.      * @return the String, converted to lowercase.
  643.      * @see Character#toLowerCase
  644.      * @see String#toUpperCase
  645.      */
  646.     public String toLowerCase() {
  647.     int len = count;
  648.     char trt[] = Character.downCase;
  649.     int i, c;
  650.     for (i = 0 ; i < len ; i++) {
  651.         c = value[offset+i];
  652.         if ((c < 256) && (trt[c] != c)) {
  653.         break;
  654.         }
  655.     }
  656.     if (i >= len) {
  657.         return this;
  658.     }
  659.     char buf[] = new char[len];
  660.     for (i = 0 ; i < len ; i++) {
  661.         c = value[offset+i];
  662.         buf[i] = (c < 256) ? trt[c] : (char)c;
  663.     }
  664.     return new String(buf);
  665.     }
  666.  
  667.     /**
  668.      * Converts all of the characters in this String to upper case.
  669.      * @return the String, converted to uppercase.
  670.      * @see Character#toUpperCase
  671.      * @see String#toLowerCase
  672.      */
  673.     public String toUpperCase() {
  674.     int len = count;
  675.     char trt[] = Character.upCase;
  676.     int i, c;
  677.     for (i = 0 ; i < len ; i++) {
  678.         c = value[offset+i];
  679.         if ((c < 256) && (trt[c] != c)) {
  680.         break;
  681.         }
  682.     }
  683.     if (i >= len) {
  684.         return this;
  685.     }
  686.     char buf[] = new char[len];
  687.     for (i = 0 ; i < len ; i++) {
  688.         c = value[offset+i];
  689.         buf[i] = (c < 256) ? trt[c] : (char)c;
  690.     }
  691.     return new String(buf);
  692.     }
  693.  
  694.     /**
  695.      * Trims leading and trailing whitespace from this String.
  696.      * @return the String, with whitespace removed.
  697.      */
  698.     public String trim() {
  699.     int len = count;
  700.     int st = 0;
  701.     while ((st < len) && (value[offset + st] <= ' ')) {
  702.         st++;
  703.     }
  704.     while ((st < len) && (value[offset + len - 1] <= ' ')) {
  705.         len--;
  706.     }
  707.     return ((st > 0) || (len < count)) ? substring(st, len) : this;
  708.     }
  709.  
  710.     /**
  711.      * Converts this String to a String.
  712.      * @return the String itself.
  713.      */
  714.     public String toString() {
  715.     return this;
  716.     }
  717.  
  718.     /**
  719.      * Converts this String to a character array. This creates a new array.
  720.      * @return     an array of characters.
  721.      */
  722.     public char[] toCharArray() {
  723.     int i, max = length();
  724.     char result[] = new char[max];
  725.     getChars(0, max, result, 0);
  726.     return result;
  727.     }
  728.  
  729.     /**
  730.      * Returns a String that represents the String value of the object.
  731.      * The object may choose how to represent itself by implementing
  732.      * the toString() method.
  733.      * @param obj    the object to be converted
  734.      */
  735.     public static String valueOf(Object obj) {
  736.     return (obj == null) ? "null" : obj.toString();
  737.     }
  738.  
  739.     /**
  740.      * Returns a String that is equivalent to the specified character array.
  741.      * Uses the original array as the body of the String (ie. it does not
  742.      * copy it to a new array).
  743.      * @param data    the character array
  744.      */
  745.     public static String valueOf(char data[]) {
  746.     return new String(data);
  747.     }
  748.  
  749.     /**
  750.      * Returns a String that is equivalent to the specified character array.
  751.      * @param data    the character array
  752.      * @param offset    the offset into the value of the String
  753.      * @param count     the length of the value of the String
  754.      */
  755.     public static String valueOf(char data[], int offset, int count) {
  756.     return new String(data, offset, count);
  757.     }
  758.  
  759.     
  760.     /**
  761.      * Returns a String that is equivalent to the specified character array.
  762.      * It creates a new array and copies the characters into it.
  763.      * @param data    the character array
  764.      * @param offset    the offset into the value of the String
  765.      * @param count     the length of the value of the String
  766.      */
  767.     public static String copyValueOf(char data[], int offset, int count) {
  768.     char str[] = new char[count];
  769.     System.arraycopy(data, offset, str, 0, count);
  770.     return new String(str);
  771.     }
  772.  
  773.     /**
  774.      * Returns a String that is equivalent to the specified character array.
  775.      * It creates a new array and copies the characters into it.
  776.      * @param data    the character array
  777.      */
  778.     public static String copyValueOf(char data[]) {
  779.     return copyValueOf(data, 0, data.length);
  780.     }
  781.  
  782.     /**
  783.      * Returns a String object that represents the state of the specified boolean.
  784.      * @param b    the boolean
  785.      */
  786.     public static String valueOf(boolean b) {
  787.     return b ? "true" : "false";
  788.     }
  789.  
  790.     /**
  791.      * Returns a String object that contains a single character
  792.      * @param c the character
  793.      * @return     the resulting String.
  794.      */
  795.     public static String valueOf(char c) {
  796.     char data[] = {c};
  797.     return new String(data);
  798.     }
  799.  
  800.     /**
  801.      * Returns a String object that represents the value of the specified integer.
  802.      * @param i    the integer
  803.      */
  804.     public static String valueOf(int i) {
  805.         return Integer.toString(i, 10);
  806.     }
  807.  
  808.     /**
  809.      * Returns a String object that represents the value of the specified long.
  810.      * @param l    the long
  811.      */
  812.     public static String valueOf(long l) {
  813.         return Long.toString(l, 10);
  814.     }
  815.  
  816.     /**
  817.      * Returns a String object that represents the value of the specified float.
  818.      * @param f    the float
  819.      */
  820.     public static String valueOf(float f) {
  821.     return Float.toString(f);
  822.     }
  823.  
  824.     /**
  825.      * Returns a String object that represents the value of the specified double.
  826.      * @param d    the double
  827.      */
  828.     public static String valueOf(double d) {
  829.     return Double.toString(d);
  830.     }
  831.  
  832.  
  833.     /**
  834.      * The set of internalized Strings.
  835.      */
  836.     private static Hashtable InternSet;
  837.  
  838.     /**
  839.      * Returns a String that is equal to this String
  840.      * but which is guaranteed to be from the unique String pool.  For example:
  841.      * <pre>s1.intern() == s2.intern() <=> s1.equals(s2).</pre>
  842.      */
  843.     public String intern() {
  844.     if (InternSet == null) {
  845.         InternSet = new Hashtable();
  846.     }
  847.     String s = (String) InternSet.get(this);
  848.     if (s != null) {
  849.         return s;
  850.     }
  851.     InternSet.put(this, this);
  852.     return this;
  853.     }
  854.  
  855.     /**
  856.      * Compute the length of this string's UTF encoded form.
  857.      */
  858.     int utfLength() {
  859.     int limit = offset + count;
  860.     int utflen = 0;
  861.     for (int i = offset; i < limit; i++) {
  862.         int c = value[i];
  863.         if ((c >= 0x0001) && (c <= 0x007F)) {
  864.         utflen++;
  865.         } else if (c > 0x07FF) {
  866.         utflen += 3;
  867.         } else {
  868.         utflen += 2;
  869.         }
  870.     }
  871.     return utflen;
  872.     }
  873. }
  874.